<?php
//基础题1（1）
echo "This is truth:\"php is the best language!\"<br>";
//基础题1（2）看不懂题目就瞎写写
$txt="This is truth:\"php is the best language!\"";
echo "$txt 是变量<br>";
echo "\$a 是变量<br><br>";

//基础题2
$amount=1000;
$rate1=0.0225;
$rate2=0.0279;
$amount1=$amount*(1+$rate1);
$amount2=$amount*(1+$rate2);
echo "一年后的本息总额为：$amount1<br>";
echo "两年后的本息总额为：".$amount2."<br><br>";

//基础题3
define("pi",3.14);
define("user","root");
echo pi,"<br>",user,"<br><br>";

//函数,我用ab了，请不要打死我，这就是标准系数嘛
function equation($a,$b,$c)
{
if ($a==0)
	echo "请输入非零二次项系数<br><br>";
else 
	{
		$delta=$b*$b-(4*$a*$c);
		if ($delta<0)
			echo "该方程无实数根<br><br>";
		else if ($delta==0)
		{
			$x=-$b/(2*$a);
			echo "x1=x2=$x <br><br>";
		}	
		else if ($delta>0) 
		{
			$x1=(-$b+sqrt($delta))/(2*$a);
			$x2=(-$b-sqrt($delta))/(2*$a);
			echo "x1=$x1 <br>";
			echo "x2=".$x2."<br><br>";
		}
	}
}

$a=$_POST["a"];
$b=$_POST["b"];
$c=$_POST["c"];
equation($a,$b,$c);

//数组1
$stu = array("no"=>"23333333","name"=>"ruby","sex"=>"女","tele"=>888888888);
foreach ($stu as $key => $value) 
{
	echo $key."=>".$value."<br>";
}
echo "<br>";

//数组2
$woods=array(array("id"=>"001","name"=>"book","cost"=>"50"),array("id"=>"002","name"=>"bread","cost"=>"5"),array("id"=>"003","name"=>"bow1","cost"=>"10"));
foreach ($woods as $value)
{
	foreach ($value as $key => $val) 
	{
		echo $key."=>".$val."<br>";
	}
}
echo "<br>";

//流程控制1 好麻烦啊，果断选择流程控制3

//流程控制2
//先打印阿拉伯数字版
for ($row=1;$row<=9;$row++)
{
	for ($col=1;$col<=9;$col++)
	{
		//printf(" %d*%d=%2d\t",$row,$col,$row*$col);
		echo $row."*".$col."=".$col*$row." ";
	}
	echo "<br>";
}
echo "<br>";

//再打印汉语版，实在不知道咋整，暴力解决
$num=array(1=>"一",2=>"二",3=>"三",4=>"四",5=>"五",6=>"六",7=>"七",8=>"八",9=>"九",10=>"一十",12=>"十二",14=>"十四",15=>"十五",16=>"十六",18=>"十八",20=>"二十",21=>"二十一",24=>"二十四",25=>"二十五",27=>"二十七",28=>"二十八",30=>"三十",32=>"三十二",35=>"三十五",36=>"三十六",40=>"四十",42=>"四十二",45=>"四十五",48=>"四十八",49=>"四十九",54=>"五十四",56=>"五十六",63=>"六十三",64=>"六十四",72=>"七十二",81=>"八十一");
for ($row=1;$row<=9;$row++)
{
	for ($col=1;$col<=$row;$col++)
	{
		if ($row*$col<10)
		 echo $num[$col].$num[$row]."得".$num[$row*$col]." ";
		else
		{
			printf("%s%s%s ",$num[$col],$num[$row],$num[$row*$col]);
		}
	}
	echo "<br>";
}
echo "<br>";

//流程控制3 老老实实一级一级确定
function chargeCalculate($charge)
{
	if ($charge<0)
	echo ("WARNING:实收小于应收<br><br>");
	else
	{
		$amount_100=($charge-$charge%100)/100;
		$charge=$charge-100*$amount_100;
		$amount_50=($charge-$charge%50)/50;
		$amount_20=($charge-$charge%20)/20;
		$charge=$charge-20*$amount_20;
		$amount_10=($charge-$charge%10)/10;
		$charge=$charge-10*$amount_10;
		$amount_5=($charge-$charge%5)/5;
		$amount_1=$charge-5*$amount_5;
		echo "需要100元人民币".$amount_100."张<br>";
		echo "需要50元人民币".$amount_50."张<br>";
		echo "需要20元人民币".$amount_20."张<br>";
		echo "需要10元人民币".$amount_10."张<br>";
		echo "需要5元人民币".$amount_5."张<br>";
		echo "需要1元人民币".$amount_1."张<br><br>";
	}
}
$receipts=$_POST["receipts"];
$receivable=$_POST["receivable"];
$charge=$receipts-$receivable;
chargeCalculate($charge);

//流程控制4.1
function printGrade1($stud)
{
	ksort($stud);
	$maxkey= array_search(end($stud),$stud);
	for ($i=0;$i<=$maxkey;$i++)
	{
		if (array_key_exists($i,$stud)) 
			echo $i."=>".$stud[$i]."<br>";
		else 
			continue;
	}
}
$stud = array(0=>88,1=>60,3=>99,5=>66);
printGrade1($stud);
echo "<br>";

//流程控制4.2
function printGrade2($stud)
{
	$i=0;
	while (array_key_exists($i,$stud))
	{
		echo $i."=>".$stud[$i]."<br>";
		$i++;
	}
	if ($i<=count($stud))
		echo "Error:".$i."号考生编号不存在<br>";
}
$stud = array(0=>88,1=>60,3=>99,5=>66);
printGrade2($stud);

?>